cnr := "cargo nextest run --no-fail-fast"
cita := "cargo insta test --accept"
kcl_lib_flags := "-p kcl-lib --features artifact-graph"

# Run the same lint checks we run in CI.
lint:
    cargo clippy --workspace --all-targets --all-features -- -D warnings
    # Ensure we can build without extra feature flags.
    cargo clippy -p kcl-lib --all-targets -- -D warnings

lint-fix:
    cargo clippy --workspace --all-targets --all-features --fix

# Run the stdlib docs generation
redo-kcl-stdlib-docs-no-imgs:
    rm -f ../docs/kcl-std/types/*
    rm -f ../docs/kcl-std/functions/*
    rm -f ../docs/kcl-std/modules/*
    rm -f ../docs/kcl-std/consts/*
    EXPECTORATE=overwrite {{cnr}} {{kcl_lib_flags}} docs::gen_std_tests::test_generate_stdlib

# Generate the stdlib image artifacts
# Then run the stdlib docs generation
redo-kcl-stdlib-docs:
    rm -f ../docs/kcl-std/types/*
    rm -f ../docs/kcl-std/functions/*
    rm -f ../docs/kcl-std/modules/*
    rm -f ../docs/kcl-std/consts/*
    TWENTY_TWENTY=update {{cnr}} {{kcl_lib_flags}} -- kcl_test_example
    TWENTY_TWENTY=update {{cnr}} {{kcl_lib_flags}} docs::kcl_doc::test::kcl_test_examples
    EXPECTORATE=overwrite {{cnr}} {{kcl_lib_flags}} -- docs::gen_std_tests::test_generate_stdlib
    EXPECTORATE=overwrite {{cnr}} {{kcl_lib_flags}} -- generate_settings_docs

# Copy a test KCL file from executor tests into a new simulation test.
copy-exec-test-into-sim-test test_name:
    mkdir -p kcl-lib/tests/{{test_name}}
    cp kcl-lib/e2e/executor/inputs/{{test_name}}.kcl kcl-lib/tests/{{test_name}}/input.kcl
    zoo kcl fmt -w kcl-lib/tests/{{test_name}}/input.kcl
    just new-sim-test {{test_name}}

# Create a new, empty KCL deterministic simulation test case.
new-sim-test test_name render_to_png="true":
    mkdir kcl-lib/tests/{{test_name}}
    touch kcl-lib/tests/{{test_name}}/input.kcl
    # Add the various tests for this new test case.
    cat kcl-lib/tests/simtest.tmpl | sed "s/TEST_NAME_HERE/{{test_name}}/" | sed "s/RENDER_TO_PNG/{{render_to_png}}/" >> kcl-lib/src/simulation_tests.rs

# Run a KCL deterministic simulation test case and accept output.
overwrite-sim-test-sample test_name:
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::kcl_samples::parse_{{test_name}}
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::kcl_samples::unparse_{{test_name}}
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::kcl_samples::kcl_test_execute_{{test_name}}
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::kcl_samples::test_after_engine

overwrite-sim-test test_name:
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::{{test_name}}::parse
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::{{test_name}}::unparse
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::{{test_name}}::kcl_test_execute
    [ {{test_name}} != "kcl_samples" ] || ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::{{test_name}}::test_after_engine

# Regenerate all the simulation test output.
redo-sim-tests:
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests
    # This copies PNG files generated during tests to the public folder. It
    # needs to happen *after* they're all generated.
    ZOO_SIM_UPDATE=always EXPECTORATE=overwrite TWENTY_TWENTY=update {{cita}} {{kcl_lib_flags}} --no-quiet -- simulation_tests::kcl_samples::test_after_engine

test:
    cargo install cargo-nextest
    {{cnr}} --workspace --features=artifact-graph

[working-directory: 'kcl-python-bindings']
python-test:
    just test

[working-directory: 'kcl-python-bindings']
python-test-filter name:
    just test-filter {{name}}

bump-kcl-crate-versions bump='patch':
    # First build the kcl-bumper tool.
    cargo build -p kcl-bumper
    ./target/debug/kcl-bumper --bump {{bump}}
    cargo check -p kcl-bumper # this way Cargo.lock gets updated

publish-kcl version:
    git tag kcl-{{version}} -m "Release kcl-{{version}}"
    cargo publish -p kcl-derive-docs
    cargo publish -p kcl-directory-test-macro
    cargo publish -p kcl-error
    cargo publish -p kcl-api
    cargo publish -p kcl-lib
    cargo publish -p kcl-test-server
    # We push the tag at the end of publish since pushing the tag
    # will trigger CI to release the kcl python bindings.
    git push origin kcl-{{version}}

[working-directory: 'kcl-python-bindings']
python-install-deps:
    @echo "🔧 Installing Python dependencies..."
    uv sync --extra test
    @echo "✅ Python dependencies installed successfully"

[working-directory: 'kcl-python-bindings']
python-stubs:
    @echo "📝 Generating type stubs with pyo3-stubgen..."
    cargo run --bin stub_gen
    mv zoo_kcl.pyi kcl.pyi
    @echo "✅ Type stubs generated successfully"

# Verify stub files are included in wheel
[working-directory: 'kcl-python-bindings']
python-verify-stubs:
    @echo "🔍 Verifying stub files in wheel..."
    uvx maturin build --release
    python -m zipfile -l ../target/wheels/*.whl | grep "\.pyi"
    @echo "✅ Stub files verified in wheel package"
